
package cn.org.seventh.common;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since Nov 30, 2007 4:15:19 PM
 */
public class FileUtil {

    private final static Log log = LogFactory.getLog(FileUtil.class);

    public static boolean deleteFiles(Collection fileCollection) {
        boolean success = true;
        if (fileCollection != null) {
            for (Iterator all = fileCollection.iterator(); all.hasNext();) {
                File file = (File) all.next();
                if (file != null) {
                    try {
                        if (!file.delete())
                            success = false;
                    } catch (Throwable t) {
                        log.debug("Failed to delete \"" + file.getPath()
                            + "\"!", t);
                        success = false;
                    }
                }
            }
        }
        return success;
    }

    public static boolean deleteFile(File file) {
        boolean success = true;
        if (file != null && file.exists()) {
            try {
                if (!file.delete())
                    success = false;
            } catch (Throwable t) {
                log.debug("Failed to delete \"" + file.getPath() + "\"!", t);
                success = false;
            }
        }
        return success;
    }

    public static boolean deleteDir(File path) {
        boolean success = true;
        if (path != null && path.exists()) {
            if (path.isDirectory()) {
                File[] files = path.listFiles(new FileFilter() {

                    public boolean accept(File f) {
                        return f.isFile();
                    }
                });
                for (int i = 0; i < files.length; i++) {
                    if (files[i] != null) {
                        try {
                            if (!files[i].delete())
                                success = false;
                        } catch (Throwable t) {
                            log.debug("Failed to delete \""
                                + files[i].getPath() + "\"!", t);
                            success = false;
                        }
                    }
                }
                File[] paths = path.listFiles(new FileFilter() {

                    public boolean accept(File f) {
                        return f.isDirectory();
                    }
                });
                for (int i = 0; i < paths.length; i++) {
                    if (paths[i] != null) {
                        if (!deleteDir(paths[i]))
                            success = false;
                    }
                }
            }
            try {
                if (!path.delete())
                    success = false;
            } catch (Throwable t) {
                log.debug("Failed to delete \"" + path.getPath() + "\"!", t);
                success = false;
            }
        }
        return success;
    }

    public static void copyFile(File src, File dest) throws IOException {
        if (src == null || !src.exists())
            throw new FileNotFoundException("The source file not fould!");
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src), 8192);
            out = new BufferedOutputStream(new FileOutputStream(dest), 8192);
            byte[] buf = new byte[8192];
            int len = 0;
            while ((len = in.read(buf, 0, buf.length)) != -1) {
                out.write(buf, 0, len);
            }
            out.flush();
            in.close();
            in = null;
            out.close();
            out = null;
        } catch (Throwable t) {
            Throwable st = t;
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ioe) {
                    st = ioe;
                }
            }
            if (out != null) {
                out.close();
            };
            log.debug("Failed to copy \"" + src.getPath() + "\" to \""
                + dest.getPath() + "\"", t);
            if (st != null)
                throw new IOException(st.getMessage());
        }
    }
}
